package org.kernelab.basis.socket;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import org.kernelab.basis.Tools;

public class PortScanner
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		String host = "localhost";
		new PortScanner().scan(host);
	}

	public void scan(String host)
	{
		Socket socket = null;

		for (int port = 1; port < 1024; port++) {
			try {
				socket = new Socket(host, port);
				Tools.debug("Port: " + port + " is in use.");
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				Tools.debug("Can not connect to port: " + port);
			} finally {
				try {
					if (socket != null) {
						socket.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
